/*
 * Copyright (c) 2024 endless-sky
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef EL_WS_PROTOCOL_SERVICE_H
#define EL_WS_PROTOCOL_SERVICE_H

#include "nlohmann/json.hpp"
#include "utils_noncopyable.h"
#include "web_server.h"
#include "utils_block_queue.h"
#include <thread>
#include <atomic>

namespace El {
namespace Restful {

class WebSocketProtocolService {
    DISALLOW_COPY_AND_MOVE(WebSocketProtocolService);

public:
    static WebSocketProtocolService &GetInstance();

    bool Start();
    void Stop();

private:
    WebSocketProtocolService() = default;
    ~WebSocketProtocolService();

    void HandleWebSocketEvent(std::shared_ptr<El::WebServer::WebSocketEvent> &event);
    void ProcessWebSocketEvents();

    std::unique_ptr<El::Utils::BlockingQueue<std::shared_ptr<El::WebServer::WebSocketEvent>>> event_queue_{nullptr};
    std::unique_ptr<std::thread> process_thread_{nullptr};
    std::atomic<bool> running_{false};
};
} // namespace Restful
} // namespace El

#endif // EL_WS_PROTOCOL_SERVICE_H
